﻿using System;
using System.Text;
using System.Web.UI.WebControls;
using Controller;


public partial class CategoryPage : System.Web.UI.Page
{
    public static string _idCat;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var idCat = Request.QueryString["IdCategory"];            
            if (!string.IsNullOrEmpty(idCat))
            {
                string pageNum;
                // spilit idCat de lay number
                string[] arrIdCat = idCat.Split('/');

                if (arrIdCat.Length == 3)
                {
                    _idCat = arrIdCat[0];
                    pageNum = arrIdCat[2];
                }
                else
                {
                    _idCat = idCat;
                    pageNum = "1";
                }
                

                // var pageNum = Request.QueryString["Numpage"];
                if (!string.IsNullOrEmpty(pageNum))
                {
                    BindData(Convert.ToInt32(pageNum.Replace("'", "")), _idCat);
                }
                else
                {
                    BindData(1, _idCat);
                }
            }
        }
    }

    public void BindData(int pageNo, string idCat)
    {
        var postController = new PostController();
        var data = postController.GetListPostsByIdCategory(idCat);
        // paging        
        var currentPage = pageNo - 1;
        litCurrentPage.Text = (currentPage + 1).ToString();
        var nextPage = pageNo + 1;
        litNextpage.Text = nextPage.ToString();
        linkNext.NavigateUrl = "~/category/" + _idCat + "/page/" + nextPage;
        var prePage = pageNo - 1;
        litPrepage.Text = prePage.ToString();
        linkPre.NavigateUrl = "~/category/" + _idCat + "/page/" + prePage;

        var pagedDataSource = new PagedDataSource
        {
            DataSource = data,
            AllowPaging = true,
            PageSize = 5,
            CurrentPageIndex = currentPage
        };
        var totalPage = pagedDataSource.PageCount;
        litTotalPage.Text = (pagedDataSource.PageCount).ToString();

        if (prePage <= 0)
        {
            linkPre.NavigateUrl = "~/category/" + _idCat + "/page/#";
        }

        if (nextPage > totalPage)
        {
            linkNext.NavigateUrl = "~/category/" + _idCat + "/page/#";
        }

        repeaterPosts.DataSource = pagedDataSource;
        // repeaterPosts.DataSource = data;
        repeaterPosts.DataBind();
    }

    public string SpilitTag(string strTags)
    {
        var stringBuilder = new StringBuilder();
        string[] tags = strTags.Split(',');
        foreach (var tag in tags)
        {
            stringBuilder.Append("<a class=\"badge\" href=\"../label/" + tag + "\">" + tag + "</a> ");
        }

        return stringBuilder.ToString();
    }

    public string GetNameofUser(int idUser)
    {
        var userController = new UserController();
        var model = userController.GetInfoUser(idUser);
        return model[0].FullName;
    }

    public string GetNameofCategory(string idCategory)
    {
        var categoryController = new CategoryController();
        var model = categoryController.GetInfoOfCategory(idCategory);
        return model[0].Name;
    }
}